const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
const baseurl="https://m.sapiki.com"
const baseUrl="https://m.sapiki.com/api/"
// const baseurl = "http://127.0.0.1";
// const baseUrl = "http://127.0.0.1/api/";
var header = {
  'content-type': 'application/json'
}
const Http = {
  get: function (url, data, isLoading) {
    if (isLoading) {
      wx.showLoading({
        title: '加载中'
      });
    }
    var request_data={
      request_data:data
    }
    return new Promise((resolve, reject) => {
      wx.request({
        url: baseUrl + url,
        header: header,
        method: 'GET',
        data: request_data,
        success: function (res) {
          if (isLoading) {
            wx.hideLoading();
          }
          let code = res.data.code;
          resolve(res.data)
        },
        fail: function (err) {
          if (isLoading) {
            wx.hideLoading();
          }
          reject(err);
        }
      });
    }).catch(function (err) {
      // wx.showToast({
      //   title: String(err),
      //   icon: 'none',
      //   duration: 2000
      // });
    });
  },
  post: function (url, data, isLoading, isUpdata, isUserInfo) {
    if (isLoading) {
      wx.showLoading({
        title: '加载中..'
      });
    }
    
    var request_data={
      request_data:data
    }
    return new Promise((resolve, reject) => {
      wx.request({
        url: baseUrl + url,
        header: header,
        method: 'POST',
        data: request_data,
        success: function (res) {
          if (isLoading) {
            wx.hideLoading();
          }
          let status = res.data.status;
          if(status == 3){
            wx.hideLoading({
              success: (res) => {},
            })
            
			if(res.data.message.indexOf('您已下线')!==-1){
				wx.showModal({
				  title: '提示',
				  content: res.data.message,
				  success: function (res) {
				    wx.clearStorageSync()
				    wx.switchTab({
						url:'/pages/index/index'
					})
				  }
				})
			}
			
			if(url=='store/containerL' || url=='user/find'){
				return
			}
			
          }else{
            resolve(res.data)
          }
          
          // if(res)
          // if (code === 200) {
          //   resolve(res.data);
          // } else if(code === 0){
          //   resolve(res.data);
          // }else if (code === 201) {
            
          // } else {
          //   resolve(res.data);
          //   reject(res.data.error);
          // }
        },
        fail: function (err) {
          if (isLoading) {
            wx.hideLoading();
          }
          reject(err);
        }
      });
    }).catch(function (err) {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      });
    });
  },
}

module.exports = {
  formatTime,
  get: Http.get,
  post: Http.post,
  baseUrl,
  baseurl
}
